import React, {Component} from 'react';
import './index.css'
import PropTypes from "prop-types";

class Item extends Component {
    static propTypes={
        updateTodos:PropTypes.func.isRequired,
        deleteTodo:PropTypes.func.isRequired
    }
    state = {
        flag: false
    }
    handleMouse = (flag) => {
        return () => {
            this.setState({
                flag
            })
        }
    }
    handleCheck=(id)=>{
        return (event)=>{
            this.props.updateTodos(id,event.target.checked)
        }
    }
    handleDelete=(event,id)=>{
        if (window.confirm('确定删除吗？')){
            this.props.deleteTodo(id)
        }
    }
    render() {
        const {id,name, checked} = this.props
        const {flag} = this.state
        return (
            <li style={{background: flag ? '#ddd' : 'white'}} onMouseEnter={this.handleMouse(true)}
                onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={checked} onChange={this.handleCheck(id)}/>
                    <span>{name}</span>
                </label>
                <button onClick={event => this.handleDelete(event,id)} className='btn btn-danger' style={{display: flag ? 'block' : 'none'}}>删除</button>
            </li>
        );
    }
}

export default Item